package com.home.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.home.school.bean.TComment;
import com.home.school.bean.TLog;
import com.home.school.bean.TUser;
import com.home.school.service.TCommentService;
import com.home.school.model.R;
import com.home.school.service.TUserService;
import com.home.school.vo.CommentListVO;
import com.home.school.vo.CommentSelectVO;
import com.home.school.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private TCommentService tCommentService;
    @Resource
    private TUserService tUserService;

    /**
     * 删除评论
     * @param id
     * @return
     */
    @PostMapping("/deleted/{id}")
    public R list(@PathVariable("id") Integer id){
        return R.ok().data(tCommentService.removeById(id));
    }

    /**
     * 列表查询 评论
     * @param bean
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody CommentListVO bean){
        Page<TComment> page = new Page<>(bean.getPageNum(),bean.getPageSize());
        QueryWrapper<TComment> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(bean.getUsername())){
            wrapper.eq("username", bean.getUsername());
        }
        return R.ok().data(  tCommentService.page(page,wrapper));
    }

    /**
     * 评论
     * @param comment
     * @return
     */
    @PostMapping("/comment")
    public R add(@RequestBody TComment comment){
        // 判断内容是否全
        if(StringUtils.isEmpty(comment.getContent())){
            return R.error("评论内容不能为空");
        }
        tCommentService.save(comment);
        return R.ok();
    }

    /**
     * 评论查询，递归查询出所有子评论
     * @param commentSelectVO
     * @return
     */
    @PostMapping("/select")
    public R select(@RequestBody CommentSelectVO commentSelectVO){
        QueryWrapper query = new QueryWrapper();
        query.isNull("parent_id");
        query.eq("order_id",commentSelectVO.getOrderId());
        List<TComment> list = tCommentService.list(query);
        // 第一层
        if(list.size() > 0 ){
            for (int i = 0; i < list.size(); i++) {
                // 递归查询下层
                getList(list.get(i));
            }
        }

        for (TComment tComment : list) {
            if(Objects.nonNull(tComment)){
                TUser tUser = tUserService.getById(tComment.getUsername());
                if(Objects.nonNull(tUser)){
                    tComment.setUsernames(tUser.getUsername());
                }

                TUser replyTo = tUserService.getById(tComment.getReplyTo());
                if(Objects.nonNull(replyTo)){
                    tComment.setReplyTos(replyTo.getUsername());
                }
            }
        }
        return  R.ok().data(list);
    }

    // 递归查询下级评论
    private void getList(TComment comment) {
        QueryWrapper query = new QueryWrapper();
        query.eq("parent_id",comment.getId());
        List<TComment> comments = tCommentService.list(query);
        // 判断comments是否为空
        if(comments.size() > 0 ){
            comment.setCommentList(comments);
            for (int i = 0; i < comments.size(); i++) {
                // 递归查询下层
                TComment tComment = comments.get(i);
                fillUserName(tComment);
                getList(tComment);
            }
        }
    }

    private void fillUserName(TComment comment){
        TUser tUser = tUserService.getById(comment.getUsername());
        if(Objects.nonNull(tUser)){
            comment.setUsernames(tUser.getUsername());
        }

        TUser replyTo = tUserService.getById(comment.getReplyTo());
        if(Objects.nonNull(replyTo)){
            comment.setReplyTos(replyTo.getUsername());
        }
    }

}
